<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;

use app\admin\model\Role as RoleModel;


class Con3 extends Con5
{
    public function func1()
    {

        $list = Db::table('role')->field(['id', 'name', 'comment', 'status'])->order('id', 'asc')->select();
        return view('admin@con3/func1', [
            'list' => $list,
            'name' => $this->name
        ]);
    }

    public function func2()
    {

        return view('admin@con3/func2',[
            'name' => $this->name
        ]);
    }

    public function save(Request $request)
    {
        //ORM
        //实例化model
        $role = new RoleModel($request->post());

        //  验证器 验证数据
        $check = $this->validate($request->post(), 'admin/Role');

        if ($check !== true) {
            $this->error($check);
            return;
        }

//        halt($request->post());

        // 添加数据,并过滤非数据表字段
        // 返回 写入的记录数
        $result = $role->allowField(true)->save();
//        var_dump($user);

        // 获取自订ID
        echo $role->id;

        // 判断执行情况
        if ($result > 0) {
            return $this->success('添加成功', url('admin/con2/func1'));
        } else {
            return $this->error('添加失败,请重试!');
        }
    }

    public function rulelist($id)
    {

        $sql = "SELECT `name`,`id` FROM `rule`";
        $list = Db::query($sql);
        $data = Db::table('role_rule')->field('rule_id')->where('role_id = ' . $id)->select();
//        halt($data);
        foreach ($list as $k => $v) {
            $list[$k]['check'] = null;
            foreach ($data as $y) {
                if ($v['id'] == $y['rule_id']) {
                    $list[$k]['check'] = 'checked';

                }
            }
        }


//        halt($list);
        $row = Db::table('role')->field(['name', 'id'])->find($id);
        return view('admin@con3/rulelist', [
            'list' => $list,
            'row' => $row,
            'name' => $this->name
        ]);
    }

    public function giveRule(Request $request)
    {
        $post = $request->post();
//        halt($post);
        $role_id = $post['role_id'];
        //查询角色所有权限 并删除
        Db::table('role_rule')->where('role_id = ' . $role_id)->delete();
        //执行添加权限
        foreach ($post as $k => $v) {
            if ($k !== 'role_id') {
                $data = [
                    'role_id' => $role_id,
                    'rule_id' => $v
                ];
                $result = Db::table('role_rule')->insert($data);
            }
        }
        //如果不选权限默认给与浏览用户权限
        $data = Db::table('role_rule')->where('role_id = ' . $role_id)->select();
        if (!$data) {
            $data = [
                'role_id' => $role_id,
                'rule_id' => 1
            ];
            $result = Db::table('role_rule')->insert($data);
        }


        if ($result > 0) {
            return $this->success('添加成功', url('admin/con2/func1'));
        } else {
            return $this->error('添加失败,请重试!');
        }
//        halt($post);
    }

    public function delete()
    {
        echo '111111';
    }

    public function edit()
    {
        echo '11111';
    }


}

